<?php

namespace app\home\controller;
use app\common\validate\merchant\MerchantMemberSalesmanAddressValid;
use app\home\loader\Services;
use think\Exception;

/**
 * 用户绑定收货地址
 */
class MerchantMemberSalesmanAddress extends ApiBase
{

    /**
     *  列表
     */
    public function page()
    {
        try {
            $options = [
                'user_id' => $this->shopUser['user_id'],
                'mer_id' => $this->merId,
                'user_type' => USER_TYPE_SALESMAN,
            ];
            $res = Services::MerchantMemberSalesmanAddress()->Page($options);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  详情
     */
    public function info()
    {

        $options = input('get.');
        try {
            $this->validate($options, MerchantMemberSalesmanAddressValid::class . '.info');
            $where = [
                'mer_id' => $this->merId,
                'user_id' => $this->shopUser['user_id'],
                'id' => $options['id'],
            ];
            $res = Services::MerchantMemberSalesmanAddress()->info($where);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  删除
     */
    public function del()
    {
        $options = input('post.');
        try {
            $this->validate($options, MerchantMemberSalesmanAddressValid::class . '.del');
            $where = [
                'mer_id' => $this->merId,
                'user_id' => $this->shopUser['user_id'],
                'id' => $options['id'],
            ];
            $res = Services::MerchantMemberSalesmanAddress()->del($where);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  增加/修改
     */
    public function save()
    {

        $options = input('post.');
        try {
            $this->validate($options, MerchantMemberSalesmanAddressValid::class . '.save');
            $options['user_id'] = $this->shopUser['user_id'];
            $options['mer_id'] = $this->shopUser['mer_id'];
            $options['user_type'] = USER_TYPE_SALESMAN;
            $res = Services::MerchantMemberSalesmanAddress()->save($options);
            api_result(API_SUCCESS, 'OK', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  默认地址
     */
    public function bind()
    {

        $options = input('post.');
        try {
            $this->validate($options, MerchantMemberSalesmanAddressValid::class . '.bind');
            $options = [
                'id' => $options['id'],
                'def' => 1,
                'mer_id' => $this->merId,
                'user_id' => $this->shopUser['user_id']
            ];
            Services::MerchantMemberSalesmanAddress()->save($options);
            api_result(API_SUCCESS, 'OK', ['id' => $options['id']]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

}
